package org.lc.stk.web.dto.bank;

import java.util.List;
import java.util.stream.Collectors;

import org.lc.stk.model.banks.Bank;
import org.lc.stk.model.question.attrs.DifficultySchema;
import org.lc.stk.model.question.attrs.QuestionType;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "题库响应DTO")
public class BankResponse {
    
    @Schema(description = "题库ID")
    private Integer id;

    @Schema(description = "题库名称")
    private String name;

    @Schema(description = "题库类型")
    private Integer type;

    @Schema(description = "题库状态：1-在用，0-停用")
    private Integer state;

    @Schema(description = "题型列表")
    private List<QuestionTypeInfo> questionTypes;

    @Schema(description = "难度方案信息")
    private DifficultySchemaInfo difficultySchema;

    public static BankResponse fromEntity(Bank bank) {
        BankResponse response = new BankResponse();
        response.setId(bank.getId());
        response.setName(bank.getName());
        response.setType(bank.getType());
        response.setState(bank.getState());
        
        if (bank.getQuestionTypes() != null) {
            response.setQuestionTypes(bank.getQuestionTypes().stream()
                .map(QuestionTypeInfo::fromEntity)
                .collect(Collectors.toList()));
        }
        
        if (bank.getDifficultySchema() != null) {
            response.setDifficultySchema(DifficultySchemaInfo.fromEntity(bank.getDifficultySchema()));
        }
        
        return response;
    }

    @Schema(description = "题型信息")
    public static class QuestionTypeInfo {
        @Schema(description = "题型ID")
        private Long id;

        @Schema(description = "题型名称")
        private String name;

        @Schema(description = "题型类型：SYSTEM-系统内置, USER_DEFINED-用户自定义")
        private String type;

        public static QuestionTypeInfo fromEntity(QuestionType questionType) {
            QuestionTypeInfo info = new QuestionTypeInfo();
            info.setId(questionType.getId());
            info.setName(questionType.getName());
            info.setType(questionType.getType().name());
            return info;
        }

        // Getters and Setters
        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }
    }

    @Schema(description = "难度方案信息")
    public static class DifficultySchemaInfo {
        @Schema(description = "难度方案ID")
        private Long id;

        @Schema(description = "难度方案名称")
        private String name;

        @Schema(description = "难度方案描述")
        private String description;

        @Schema(description = "难度方案类型")
        private String type;

        @Schema(description = "难度等级数量")
        private Integer levelCount;

        public static DifficultySchemaInfo fromEntity(DifficultySchema schema) {
            DifficultySchemaInfo info = new DifficultySchemaInfo();
            info.setId(schema.getId());
            info.setName(schema.getName());
            info.setDescription(schema.getDescription());
            info.setType(schema.getType().getLabel());
            info.setLevelCount(schema.getType().getLevelCount());
            return info;
        }

        // Getters and Setters
        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public Integer getLevelCount() {
            return levelCount;
        }

        public void setLevelCount(Integer levelCount) {
            this.levelCount = levelCount;
        }
    }

    // Getters and Setters
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public List<QuestionTypeInfo> getQuestionTypes() {
        return questionTypes;
    }

    public void setQuestionTypes(List<QuestionTypeInfo> questionTypes) {
        this.questionTypes = questionTypes;
    }

    public DifficultySchemaInfo getDifficultySchema() {
        return difficultySchema;
    }

    public void setDifficultySchema(DifficultySchemaInfo difficultySchema) {
        this.difficultySchema = difficultySchema;
    }
}